﻿using Moqikaka.BLL;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace OldPlayersReturn
{
    /// <summary>
    /// API 的摘要说明
    /// </summary>
    public class API : IHttpHandler
    {

        /// <summary>
        /// ProcessRequest
        /// </summary>
        /// <param name="context"></param>
        public void ProcessRequest(HttpContext context)
        {
            var result = new ResultResponse();
            //读取请求数据
            Byte[] bytes = context.Request.BinaryRead(context.Request.TotalBytes);
            String requestString = Encoding.UTF8.GetString(bytes);

            //序列化为对象
            var requestData = JsonConvert.DeserializeObject<RequestData>(requestString);

            if (requestData == null)
            {
                result.ResultCode = ResultCodeEnum.ParameterError;
                goto Flush;
            }

            //找到模块方法
            var actionInfo = BLLExecuteActions.GetAction(requestData.ModuleName, requestData.ActionName);
            if (actionInfo != null)
            {
                Object[] param={};
                try
                {
                    //参数
                    param = ParseParameterValue(requestData.Data, actionInfo.parameterType);
                }
                catch (Exception ex)
                {
                    result.ResultCode = ResultCodeEnum.ParameterError;
#if DEBUG
                    result.Message = ex.Message + ex.StackTrace;
#endif
                }
                //开始执行方法
                result = actionInfo.method.Invoke(null, param) as ResultResponse;
            }
            else
            {
                result.ResultCode = ResultCodeEnum.NotFindAction;
            }

            
        Flush:
            var responseContent = JsonConvert.SerializeObject(result);
            context.Response.Write(responseContent);
            context.Response.End();
        }

        /// <summary>
        /// 转换参数值
        /// </summary>
        /// <param name="objs">输入参数</param>
        /// <param name="types">参数类型</param>
        /// <returns>转换后的参数</returns>
        private static Object[] ParseParameterValue(List<Object> objs, Type[] types)
        {

            Object[] result = new Object[types.Length];
            for (Int32 i = 0; i < types.Length; i++)
            {
                Type tempT = objs[i].GetType();
                if (tempT == types[i])
                {
                    result[i] = objs[i];
                }
                else
                {
                    switch (types[i].ToString())
                    {
                        case "System.Boolean":
                            result[i] = Convert.ToBoolean(objs[i]);
                            break;
                        case "System.Byte":
                            result[i] = Byte.Parse(objs[i].ToString());
                            break;
                        case "System.Char":
                            result[i] = Char.Parse(objs[i].ToString());
                            break;
                        case "System.DateTime":
                            result[i] = DateTime.Parse(objs[i].ToString());
                            break;
                        case "System.Decimal":
                            result[i] = Decimal.Parse(objs[i].ToString());
                            break;
                        case "System.Double":
                            result[i] = Double.Parse(objs[i].ToString());
                            break;
                        case "System.Guid":
                            result[i] = Guid.Parse(objs[i].ToString());
                            break;
                        case "System.Int16":
                            Int16 tempInt16 = Int16.Parse(objs[i].ToString());

                            result[i] = tempInt16;
                            break;
                        case "System.Int32":
                            Int32 tempInt32 = Int32.Parse(objs[i].ToString());

                            result[i] = tempInt32;
                            break;
                        case "System.Int64":
                            Int64 tempInt64 = Int64.Parse(objs[i].ToString());

                            result[i] = tempInt64;
                            break;
                        case "System.SByte":
                            result[i] = SByte.Parse(objs[i].ToString());
                            break;
                        case "System.Single":
                            result[i] = Single.Parse(objs[i].ToString());
                            break;
                        case "System.String":
                            result[i] = objs[i].ToString();
                            break;
                        case "System.UInt16":
                            UInt16 tempUInt16 = UInt16.Parse(objs[i].ToString());

                            result[i] = UInt16.Parse(objs[i].ToString());
                            break;
                        case "System.UInt32":
                            UInt32 tempUInt32 = UInt32.Parse(objs[i].ToString());

                            result[i] = tempUInt32;
                            break;
                        case "System.UInt64":
                            UInt64 tempUInt64 = UInt64.Parse(objs[i].ToString());

                            result[i] = tempUInt64;
                            break;
                        default:
                            result[i] = objs[i];
                            break;

                    }
                }
            }

            return result;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}